package com.mitrais.training.yoga;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class GuiExerciseDay3 extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTable jTable0;
	private JScrollPane jScrollPane0;
	private JButton jButton0;
	private JButton jButton1;
	private DefaultTableModel dtm;
	private int clicked;
	private Employee list[];
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

	public GuiExerciseDay3() {
		list = new Employee[10];
		clicked = 0;
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJButton0(), new Constraints(new Leading(300, 66, 10, 10),
				new Leading(12, 12, 12)));
		add(getJButton1(), new Constraints(new Leading(300, 12, 12),
				new Leading(50, 12, 12)));
		add(getJScrollPane0(), new Constraints(new Leading(4, 284, 12, 12),
				new Bilateral(13, 12, 26, 403)));
		setSize(375, 275);
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Close");
			jButton1.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					jButton1ActionActionPerformed(event);
				}
			});
		}
		return jButton1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("Sort");
			jButton0.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					jButton0ActionActionPerformed(event);
				}
			});
		}
		return jButton0;
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getJTable0());
		}
		return jScrollPane0;
	}

	private JTable getJTable0() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		String hireDateStr1 = "2013/06/18";
		Date hireDate1 = null;
		try {
			hireDate1 = dateFormat.parse(hireDateStr1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.out.println("Invalid Date Format");
		}
		Employee emp1 = new Employee("Camp1", hireDate1, 2000000.0);
		Employee emp2 = new Employee("Camp4", hireDate1, 3000000.0);
		Employee emp3 = new Employee("Camp10", hireDate1, 4500000.0);
		Employee emp4 = new Employee("Allen", hireDate1, 2000000.0);
		Employee emp5 = new Employee("George", hireDate1, 3000000.0);
		Employee emp6 = new Employee("Jeanny", hireDate1, 4500000.0);
		Employee emp7 = new Employee("Katy", hireDate1, 2000000.0);
		Employee emp8 = new Employee("Peter", hireDate1, 3000000.0);
		Employee emp9 = new Employee("Dona", hireDate1, 4500000.0);
		Employee emp10 = new Employee("Robson", hireDate1, 2000000.0);
		list[0] = emp1;
		list[1] = emp2;
		list[2] = emp3;
		list[3] = emp4;
		list[4] = emp5;
		list[5] = emp6;
		list[6] = emp7;
		list[7] = emp8;
		list[8] = emp9;
		list[9] = emp10;
		// Arrays.sort(list);
		String colNames[] = { "Index", "Name", "Salary" };
		Object[][] data = new Object[10][colNames.length];
		for (int i = 0; i < 10; i++) {
			data[i][0] = i+1;
			data[i][1] = list[i].getName();
			data[i][2] = list[i].getSalary();
		}
		dtm = new DefaultTableModel(data, colNames);
		if (jTable0 == null) {
			jTable0 = new JTable(dtm) {
				public boolean isCellEditable(int rowIndex, int colIndex) {
					return false; // Disallow the editing of any cell
				}
			};
		}
		return jTable0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GuiExerciseDay3 frame = new GuiExerciseDay3();
				frame.setDefaultCloseOperation(GuiExerciseDay3.EXIT_ON_CLOSE);
				frame.setTitle("GuiExerciseDay3");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0ActionActionPerformed(ActionEvent event) {
		clicked++;
		if (clicked % 2 == 0) {
			Arrays.sort(list, Collections.reverseOrder());
		} else {
			Arrays.sort(list);
		}
		String colNames[] = { "Index", "Name", "Salary" };
		Object[][] data = new Object[10][colNames.length];
		for (int i = 0; i < 10; i++) {
			data[i][0] = i+1;
			data[i][1] = list[i].getName();
			data[i][2] = list[i].getSalary();
		}
		jTable0.setModel(new DefaultTableModel(data, colNames));
		((DefaultTableModel) jTable0.getModel()).fireTableDataChanged();
		jTable0.repaint();
	}

	private void jButton1ActionActionPerformed(ActionEvent event) {
		this.dispose();
	}

}
